import { reactive, onMounted, onUnmounted } from "vue";
// 普通的函数
export function useMove() {
  //   const x = ref(0);
  //   const y = ref(0);

  const position = reactive({
    x: 0,
    y: 0,
  });

  const handleKeyup = (e) => {
    switch (e.code) {
      case "ArrowRight":
        // x.value += 1;
        position.x += 1;
        break;
      case "ArrowLeft":
        // x.value -= 1;
        position.x -= 1;
        break;
      case "ArrowUp":
        // y.value -= 1;
        position.y -= 1;
        break;
      case "ArrowDown":
        // y.value += 1;
        position.y += 1;
        break;

      default:
        break;
    }
  };

  onMounted(() => {
    window.addEventListener("keyup", handleKeyup);
  });

  onUnmounted(() => {
    window.removeEventListener("keyup", handleKeyup);
  });

  //   return {
  //     x,
  //     y,
  //   };
  return position;
}
